<template>
    <div>
        <el-card shadow="always" style="width:500px;">
            <el-form label-width="80px" size="small" :rules="rules" ref="ruleForm" :model="pwd">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="user.username" autocomplete="off" disabled></el-input>
                </el-form-item>
                <el-form-item label="旧密码" prop="pwd">
                    <el-input v-model="pwd.pwd" autocomplete="off" show-password placeholder="请输入旧密码"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="newPwd">
                    <el-input v-model="pwd.newPwd" autocomplete="off" show-password placeholder="请输入新密码"></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="newPwd2">
                    <el-input v-model="pwd.newPwd2" autocomplete="off" show-password placeholder="请确认新密码"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="submit">提 交</el-button>
                </el-form-item>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Password",
        data() {
            return {
                pwd: {},
                user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
                rules: {
                    pwd: [
                        {required: true, message: '请输入旧密码', trigger: 'blur'},
                        {min: 3, message: '长度不少于3 个字符', trigger: 'blur'}
                    ],
                    newPwd: [
                        {required: true, message: '请输入新密码', trigger: 'blur'},
                        {min: 3, message: '长度不少于3 个字符', trigger: 'blur'}
                    ],
                    newPwd2: [
                        {required: true, message: '请确认新密码', trigger: 'blur'},
                        {min: 3, message: '长度不少于3 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        created() {
            this.getUser().then(res => {
                this.form = res
            })
        },
        methods: {
            async getUser() {
                return (await this.request.get("/user/username/" + this.user.username)).data
            },
            submit() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) { // 表单校验合法

                        //旧密码不正确
                        if (this.pwd.pwd !== this.user.password) {
                            this.$message.error('输入的旧密码不正确')
                            return false
                        }

                        //两次新密码不一致
                        if (this.pwd.newPwd !== this.pwd.newPwd2) {
                            this.$message.error('两次输入的密码不一致')
                            return false
                        }

                        //两次新密码一致
                        this.request.post("/user/password/" + this.user.username, this.pwd).then(res => {
                            if (res === 200) {
                                this.$router.push("/login")
                                localStorage.removeItem("user")
                                this.$message.success('密码失效,请重新登录!')
                            } else {
                                this.$message.error('系统繁忙，请稍后再试!')
                            }
                        })
                    }
                })
            }
        }
    }
</script>

<style>

</style>
